﻿using System;
using System.Diagnostics;
using System.Runtime.Remoting.Messaging;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Windows.Forms.DataVisualization.Charting;

namespace UCTT.NetFx
{
    public partial class Form1 : Form
    {
        //对照
        private string _normalStatic;

        [ThreadStatic]
        private string _threadStatic;
        private ThreadLocal<string> _threadLocal = new ThreadLocal<string>();
        //private AsyncLocal<string> _asyncLocal = new AsyncLocal<string>();

        public Form1()
        {
            InitializeComponent();

            Series series = chart1.Series[0];

            // 画样条曲线（Spline）
            series.ChartType = SeriesChartType.Spline;

            // 线宽2个像素
            series.BorderWidth = 2;

            // 线的颜色：红色
            series.Color = System.Drawing.Color.Red;

            // 图示上的文字
            series.LegendText = "演示曲线";

            // 准备数据 
            float[] values = { 95, 30, 20, 23, 60, 87, 42, 77, 92, 51, 29 };
            // 在chart中显示数据            
            int x = 0;
            foreach (float v in values)
            {
                series.Points.AddXY(x, v);
                x++;
            }

            // 设置显示范围
            ChartArea chartArea = chart1.ChartAreas[0];
            //chartArea.AxisX.Minimum = 0;
            //chartArea.AxisX.Maximum = 10;
            //chartArea.AxisY.Minimum = 0d;
            //chartArea.AxisY.Maximum = 100d;
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            //Run1();
            Run2();
        }

        private void Run1()
        {
            Parallel.For(0, 4, _ =>
            {
                var threadId = Thread.CurrentThread.ManagedThreadId;

                var value = $"这是来自线程{threadId}的数据";

                _normalStatic = value;
                _threadStatic = value;
                CallContext.SetData("value", value);
                _threadLocal.Value = value;
                //_asyncLocal.Value = value;

                Debug.WriteLine($"Run1=>Use Normal;                Thread：{threadId}; Value：{_normalStatic}");
                Debug.WriteLine($"Run1=>Use ThreadStatic;          Thread：{threadId}; Value：{_threadStatic}");
                Debug.WriteLine($"Run1=>Use CallContext;           Thread：{threadId}; Value：{CallContext.GetData("value")}");
                Debug.WriteLine($"Run1=>Use ThreadLocal;           Thread：{threadId}; Value：{_threadLocal.Value}");
                //Debug.WriteLine($"Run1=>Use AsyncLocal;            Thread：{threadId}; Value：{_asyncLocal.Value}");
            });

            //Console.Read();
        }

        private void Run2()
        {
            _threadStatic = "set";
            _threadLocal.Value = "set";
            //_asyncLocal.Value = "set";
            PrintValuesInAnotherThread();
            //Console.ReadKey();
        }

        private void PrintValuesInAnotherThread()
        {
            Task.Run(() =>
            {
                Console.WriteLine($"Run2=>ThreadStatic: {_threadStatic}");
                Console.WriteLine($"Run2=>ThreadLocal: {_threadLocal.Value}");
                //Console.WriteLine($"Run2=>AsyncLocal: {_asyncLocal.Value}");
            });
        }
    }
}